﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Common.Extension;
using Common.Model;
using Common.util;
using Dapper;

namespace Common.Dao
{
    public class BlockDao
    {
        //保存板块
        public static void BatchSaveTopics(List<Topic> topics)
        {
            var sql =
                @"replace into main_topic(topic_name,`from`,insert_date,merge_name) values (@TopicName,@From,FROM_UNIXTIME(@InsertDate),@MergeName)";
            using (var conn = DbUtils.GetHotTopicConnection())
            {
                conn.InsertMultiple(sql, topics);
            }
        }
        
        //保存板块股票
        public static void BatchSaveStocks(List<Stock> stocks)
        {
            var sql =
                @"replace into main_stocks(topic_name,stock_code,insert_date) values (@TopicName,@StockCode,FROM_UNIXTIME(@InsertDate))";
            using (var conn = DbUtils.GetHotTopicConnection())
            {

                conn.InsertMultiple(sql, stocks);
            }
        }
        
        //获取板块
        public static List<Topic> GetMainTopics()
        {
            var sql =
                @"SELECT topic_name AS TopicName,from,UNIX_TIMESTAMP(insert_date) AS InsertDate,merge_name AS MergeNaxme FROM `main_topic` where is_deleted = 0";
            using (var conn = DbUtils.GetHotTopicConnection())
            {
                return conn.Query<Topic>(sql).ToList();
            }
        }
    }
}